package com.qjq.glasses_shopping.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qjq.glasses_shopping.pojo.Permission;
import com.qjq.glasses_shopping.result.BaseResult;
import com.qjq.glasses_shopping.service.PermissionService;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台权限
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Resource
    private PermissionService permissionService;

    /**
     * 添加权限
     * @param permission 权限信息
     * @return 返回结果
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Permission permission) {
        permissionService.add(permission);
        return BaseResult.ok();
    }

    /**
     * 修改权限
     * @param permission 权限信息
     * @return 返回结果
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody Permission permission) {
        permissionService.update(permission);
        return BaseResult.ok();
    }

    /**
     * 删除权限
     * @param pid 权限id
     * @return 返回结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long pid) {
        permissionService.delete(pid);
        return BaseResult.ok();
    }

    /**
     * 根据id查询权限
     * @param pid 权限id
     * @return 返回查询到的权限信息
     */
    @GetMapping("/findById")
    public BaseResult<Permission> findById(Long pid) {
        Permission permission = permissionService.findById(pid);
        return BaseResult.ok(permission);
    }

    /**
     * 分页查询权限
     * @param page 页码
     * @param size 每页条数
     * @return 返回查询的分页结果
     */
    @GetMapping("/search")
    public BaseResult<Page<Permission>> search(int page, int size) {
        Page<Permission> permissions = permissionService.search(page, size);
        return BaseResult.ok(permissions);
    }

    /**
     * 查询全部的权限
     * @return 返回查询到的全部权限
     */
    @GetMapping("/findAll")
    public BaseResult<List<Permission>> findAll(){
        List<Permission> permissions = permissionService.findAll();
        return BaseResult.ok(permissions);
    }
}
